﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Common.Web;
using CSP.Web.Master;
using CSP.Web.UserControls;

namespace CSP.Web.MyHome.Authentication
{
    public partial class ModifyCertficate : CSPMyHome
    {
        private CSPBllFactory _factory;

        private IAuthenticationBll _authenticationBll;

        protected void Page_Load(object sender, EventArgs e)
        {

            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Authentication;


            if (!IsPostBack)
            {
                InitUI();
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            UpdateAuthentication();
        }

        /// <summary>
        /// 获取需要修改的认证信息
        /// </summary>
        /// <returns></returns>
        private CSP.Model.CSP.Authentication GetModifyAuthentication(IAuthenticationBll authenticationBll)
        {
            CSP.Model.CSP.Authentication authentication = null;
            _factory = CSPBllFactory.GetInstance();

            try
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                authentication = authenticationBll.GetByIdAndUser(id, UserProvider.UserName);

            }
            catch (Exception ex)
            {
                LoggerProvider.Error(authentication, ex);
                this.FailRedirect("操作失败", "/MyHome/Authentication/List.aspx");
            }

            if (authentication == null)
            {
                Response.Write("<script>alert('权限不足');</script>");
                throw new Exception("权限不足");
            }

            return authentication;
        }

        /// <summary>
        /// 初始化UI
        /// </summary>
        private void InitUI()
        {
            _factory = CSPBllFactory.GetInstance();
            _authenticationBll = _factory.GetAuthenticationBll();
            CSP.Model.CSP.Authentication authentication = GetModifyAuthentication(_authenticationBll);
            this.TxtName.Value = authentication.Name;
            this.TxtDescription.Value = authentication.Description;
            this.CertificateImg.Src = "~/" + authentication.Filepath + "s_" + authentication.Filename;
        }

        /// <summary>
        /// 获取已经修改过的认证信息
        /// </summary>
        /// <returns></returns>
        private CSP.Model.CSP.Authentication GetModifiedAuthentication(CSP.Model.CSP.Authentication authentication)
        {
            authentication.Name = TxtName.Value;
            authentication.Description = TxtDescription.Value;
            return authentication;
        }

        /// <summary>
        /// 调用bll层进行更新
        /// </summary>
        private void UpdateAuthentication()
        {
            _factory = CSPBllFactory.GetInstance();
            _authenticationBll = _factory.GetAuthenticationBll();
            CSP.Model.CSP.Authentication oldAuthentication = GetModifyAuthentication(_authenticationBll);
            CSP.Model.CSP.Authentication authentication = GetModifiedAuthentication(oldAuthentication);

            try
            {
                _authenticationBll.Update(authentication);
            }
            catch (Exception ex)
            {
                LoggerProvider.Error(authentication, ex);
                this.FailRedirect("提交未失败", "/MyHome/Authentication/List.aspx");
            }
            this.SuccessfullRedirect("已经提交成功", "/MyHome/Authentication/List.aspx");
        }


    }
}